require 'View/UIBase/UIBase'

UICharactorInteraction = UIBase.New(PanelNames.UICharactorInteraction);
local this=UICharactorInteraction
function this.Init(heroId)
    this.heroId=heroId
    UIBase.Init(this);
end

function this:OnCreate()
    self.btnClose=self:GetChild("CloseBtn")
    self.listInteraction =self:GetChild("list")
    self.listInteraction.itemRenderer=function(index,obj)
        this:SetInteractionData(index+1,obj)
    end
    self.listInteraction.numItems=0
    self.interactionInfo=logicMgr.HeroFavorManager.getInteractionInfo()
    self.roleData=dataTable.getRow("t_roleinfo",this.heroId)
    self:addEvent()
end

function this:OnEnable()
    this:Refresh()
end

function this:OnDisable()

end

function this:OnDestroy()

end
-------------------------------function--------------------------------------
function this:Refresh()
    self.heroData=logicMgr.HeroManager.getHeroDatabyModelId(self.heroId,true)
    self.listInteraction.numItems=#self.interactionInfo
end

function this:OnClickClose()
    self:OnBack()
end

function this:SetInteractionData(index,obj)
    local imgIcon=obj:GetChild("icon")
    local txtTitle=obj:GetChild("title")
    local btnGo=obj:GetChild("btnGo")
    local barPg=obj:GetChild("n5")
    local txtPg=obj:GetChild("num")
    local ctrlState=obj:GetController("state")
    local ctrlType=obj:GetController("type")
    local data=self.interactionInfo[index]
    local function GetInteractionData(interactId)
        local interactData={}
        for i,v in pairs(self.heroData.interactInfos)do
            if v.id==interactId then
                interactData=v
                break
            end
        end
        return interactData
    end
    local interactData=GetInteractionData(data.f_InteractionId) or {}
    if table.isNullOrEmpty(interactData)then
        interactData.times=0
    end

    local isFinish=false
    if interactData.times>=data.f_Frequency then
        isFinish=true
    end
    local num=logicMgr.HeroManager.getHeroInteractionNum()
    if isFinish then
        ctrlState.selectedIndex=1
    elseif data.f_FrequencyType==1 and num>=data.f_FrequencyCo then
        ctrlState.selectedIndex=2
    else
        ctrlState.selectedIndex=0
    end
    txtPg.text=tostring(interactData.times).."/"..tostring(data.f_Frequency)
    barPg.value=interactData.times/data.f_Frequency*100
    ctrlType.selectedIndex=data.f_InteractionType-1
    imgIcon.url="ui://CharactorUI/"..data.f_Icon
    txtTitle.text=getLanguage(data.f_InteractionTitle)
    btnGo.onClick:Clear()
    btnGo.onClick:Add(function()
        --1 出游,2 作战,3小游戏
        if data.f_InteractionType==1 then
            logicMgr.StoryMgr.ShowStoryById(self.roleData.f_RoleInteractionStory,EnumConst.StoryShowType.review)
            local info={}
            info.partnerId=self.heroData.partnerId
            info.lastLevel=self.heroData.favorLv
            info.lastExp=self.heroData.favorExp
            partnerHandler.setInteractionSaveData({info},data.f_InteractionId)
            self:OnClickClose()
        elseif data.f_InteractionType==2 then
            --现在作战仅跳转到作战界面
            if isFinish then
                UISysTips.AddMsg("已达上限")
            else
                showUI(PanelNames.UIBattleTeam, EnumConst.FightTypeConst.DUNGEON, nil, function(formations, _, Info, index)
                    local curStage = logicMgr.MapManager.getNextStage();
                    if curStage > 0 then
                        UIWaiting.WaitingShow(true)
                        if logicMgr.MapManager.getStageNeedTeamNum(curStage) > 1 then
                            dungeonHandler.sendGroupBossMessage(curStage, formations, Info, index);
                        else
                            dungeonHandler.sendBossMessage(curStage, formations[1], Info);
                        end
                        dungeonHandler.sendFormation();
                    end
                end)
                self:OnClickClose()
                closeUI(PanelNames.UICharactorInfoHero)
            end
        end

    end)
end
function this:addEvent()
    self:RegisterOnClick(self.btnClose,self.OnClickClose)
    self:RegisterEvent(Config.EventType.Fresh_HeroInteraction,self.Refresh)

end


return this;